﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;

namespace Futaba.SBus.SampleApplication
{
	class RelayCommand : ICommand
	{
		Func<object, bool> _canExecuteDelegate;
		Action<object> _executeDelegate;

		public RelayCommand(Action<object> executeDelegate, Func<object, bool> canExecuteDelegate = null)
		{
			if (executeDelegate == null)
				throw new ArgumentNullException("executeDelegate");

			_executeDelegate = executeDelegate;
			_canExecuteDelegate = canExecuteDelegate;
		}

		public bool CanExecute(object parameter)
		{
			if (_canExecuteDelegate == null)
				return true;
			return _canExecuteDelegate(parameter);
		}

		public event EventHandler CanExecuteChanged;

		public void Execute(object parameter)
		{
			_executeDelegate(parameter);
		}

		public void RaiseCanExecuteChanged()
		{
			if (CanExecuteChanged != null)
				CanExecuteChanged(this, EventArgs.Empty);
		}
	}
}
